<template>
    <div class="index-lists">
        <el-form :inline="true" :model="queryParams"  class="!border-none" shadow="never">
            <!-- 自提门店 -->
            <el-form-item label="门店名称">
                <el-input v-model="queryParams.name" placeholder="请输入门店名称" />
            </el-form-item>

            <!-- 门店状态 -->
                    <el-form-item label="门店状态" class="m-l-24">
                        <el-select v-model="queryParams.status" placeholder="请选择状态">
                            <el-option label="全部" value=""></el-option>
                            <el-option label="启用" :value="1"></el-option>
                            <el-option label="停用" :value="0"></el-option>
                        </el-select>
                    </el-form-item>

                    <!-- 搜索查询 -->
                    <el-form-item class="m-l-24">
                        <el-button type="primary" @click="resetPage">查询</el-button>
                        <el-button @click="resetParams">重置</el-button>
                    </el-form-item>
                </el-form>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['selffetchshop:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column prop="name" label="门店LOGO" min-width="80">
                    <template #default="{ row }">
                        <image-contain
                            :width="40"
                            :height="40"
                            :src="row.image"
                            :preview-src-list="[row.image]"
                            preview-teleported
                            hide-on-click-modal
                        />
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="门店名称" min-width="120" />
                <el-table-column prop="contact" label="店长" min-width="100" />
                <el-table-column prop="mobile" label="联系电话" min-width="120" />
                <el-table-column prop="detailedAddress" label="门店地址" min-width="220" />
                <el-table-column prop="createTime" label="营业时间" width="180">
                    <template #default="{ row }">
                        {{ row.businessStartTime + '-' + row.businessEndTime }}
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="门店状态" min-width="80" v-perms="['selffetchshop:status']">
                    <template #default="{ row }">
                        <el-switch
                            v-model="row.status"
                            :active-value="1"
                            :inactive-value="0"
                            @change="changeSwitchStatus($event, row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['selffetchshop:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['selffetchshop:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="selffetchshop">
import { selffetchshopDelete, selffetchshopLists, apiSelffetchShopStatus } from '@/api/selffetchshop/selffetchshop'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './shop_edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    name: '',
    image: '',
    contact: '',
    mobile: '',
    province: '',
    city: '',
    district: '',
    address: '',
    longitude: '',
    latitude: '',
    businessStartTime: '',
    businessEndTime: '',
    weekdays: '',
    status: '',
    remark: '',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: selffetchshopLists,
    params: queryParams
})



const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await selffetchshopDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

// 更改状态
const changeSwitchStatus = async (value: 0 | 1, data: any) => {
    apiSelffetchShopStatus({
            id: data.id,
            status: value
        }).catch(err => {
            getLists()
        })
}

getLists()
</script>
